package com.jboost.modules.app.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jboost.common.api.vo.Result;
import com.jboost.common.util.DateUtils;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.constant.BaseResult;
import com.jboost.modules.app.constant.CustomerDetailInfo;
import com.jboost.modules.app.dto.HsMaterialQuoteNoDto;
import com.jboost.modules.app.dto.HsPriceDto;
import com.jboost.modules.app.dto.resp.HsHuilvPriceResp;
import com.jboost.modules.app.dto.resp.HsLvPriceResp;
import com.jboost.modules.app.util.StrategyFactory;
import com.jboost.modules.system.entity.HsMaterialNeed;
import com.jboost.modules.system.mapper.HsMaterialNeedMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.utils.HttpUtils;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okio.BufferedSink;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Api(tags = "客户管理")
@RestController
@RequestMapping("/hs")
@Slf4j
public class HsController {


    String lvPriceUrl
            = "https://market.cnal.com/share/market/nh30.json";
    String huilvUrl
            = "https://www.cgbchina.com.cn/searchExchangePrice.gsp";
    @Autowired
    HsMaterialNeedMapper hsMaterialNeedMapper;
    @Autowired
    StrategyFactory strategyFactory;
    @PostMapping(value = "/priceSum")
    @ApiOperation(value = "价格统计", notes = "价格统计")
    public HsPriceDto.DataBean  priceSum(@RequestBody List<HsPriceDto.DataBean.BasePriceBean> base_price) throws Exception {
        HsPriceDto.DataBean dataBean = new HsPriceDto.DataBean();
        //todo 计算价格
        dataBean.setBig_small_price(1);
        return dataBean;
    }


    @GetMapping(value = "/huilv")
    @ApiOperation(value = "广发汇率", notes = "广发汇率")
    public HsHuilvPriceResp huilv() throws Exception {

        // 从 URL 加载 HTML 文档
        Document document = Jsoup.connect(huilvUrl).get();
        Elements elements = document.select("tr");
        Element element = elements.get(1);
        String td = element.select("td").get(7).text();
        HsHuilvPriceResp hsLvPriceResp = new HsHuilvPriceResp();
        hsLvPriceResp.setHuilv(td);
        return hsLvPriceResp;
    }
    @PostMapping(value = "/quoteNo")
    @ApiOperation(value = "根据关联编号查询", notes = "根据关联编号查询")
    public List<HsMaterialNeed> quoteNo(@RequestBody HsMaterialQuoteNoDto quoteNo) throws Exception {
        return hsMaterialNeedMapper.selectList(Wrappers.lambdaQuery(HsMaterialNeed.class).eq(HsMaterialNeed::getQuoteNo,quoteNo.getQuoteNo()));
    }

    @GetMapping(value = "/luPrice")
    @ApiOperation(value = "最新铝价", notes = "最新铝价")
    public HsLvPriceResp luPrice() throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(lvPriceUrl).get()
                .build();
        Response execute = client.newCall(request).execute();
        JSONObject data = (JSONObject)JSONUtil.fromJson(execute.body().string(), Map.class).get("data");
        JSONObject jsonObject = data.getJSONObject("10");
        String price = "";
        Long time = 1L;
        Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();
        for (Map.Entry<String, Object> objectEntry:entries){
            JSONObject value = (JSONObject)objectEntry.getValue();
            if (Long.parseLong(objectEntry.getKey())>time){
                price = value.getString("max");
                time = Long.parseLong(objectEntry.getKey());
            }

        }
        HsLvPriceResp hsLvPriceResp = new HsLvPriceResp();
        hsLvPriceResp.setPrice(price);
        return hsLvPriceResp;
    }


    @GetMapping(value = "/test")
    @ApiOperation(value = "数据同步", notes = "数据同步")
    public Result<BaseResult> test() throws Exception {
        return Result.OK((BaseResult) strategyFactory.invokeStrategy(null,"HsMaterialQuery"));
    }


}
